package huang.service;

import huang.dao.UserDao;
import huang.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Creat by 黄伟洪 on 2021/7/19 18:01
 */
@Service
public class UserService {
    @Autowired
    UserDao userDao;

    /*查询所有用户*/
    public List<User> getAllUsers() {
        return userDao.getAllUsers();
    }

    /*根据用户名和密码匹配，验证是否登录*/
    public Boolean checkLogin(String u_name, String u_password, HttpServletRequest request) {

        User user = userDao.getUserByUP(u_name, u_password);

        /*如果查询到的用户不为null*/
        if (user != null) {
            /*用户名和密码是正确*/
            /*可进行登录，用户信息存到session当中*/
            request.getSession().setAttribute("loginUser", user);
            return true;
        } else {
            return false;
        }
    }

    /*清除session信息，即退出登录*/
    public Boolean clearSession(HttpServletRequest request) {
        request.getSession().removeAttribute("loginUser");
        return true;
    }

    /*增加用户*/
    public Boolean addUser(String u_name, String u_password, String phone) {
        if (userDao.addUser(u_name, u_password, phone, false) == 1) {
            return true;
        } else {
            return false;
        }
    }

    /*用户修改密码*/
    public Boolean updatePwd(int id, String oldPwd, String newPwd) {

        /*修改密码*/
        int updateResult = userDao.updatePwd(id, oldPwd, newPwd);
        if (updateResult > 0) {
            return true;
        } else {
            return false;
        }
    }
}
